import Crypt from "../../../../utils/Crypt";
import UserModel from "../../../../models/User";

const userModel = new UserModel()

Page({
  data: {
    type: 'balance',
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    s: 1,
    account: {}
  },
  onLoad: function (options) {
    // this.setData({ id: options.id })
    this._getList()

    userModel.myAccount().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ account: data.account })
    })
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      if (this.data.s == 1) {
        userModel.myCommission({
          ...this.data,
        }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          if (data.list.length === 0) {
            this.setData({ busy: true })
          } else {
            this.setData({ busy: false })
            if (append) {
              this.setData({ list: this.data.list.concat(data.list) })
            } else {
              this.setData({ list: data.list })
            }
          }
        })
      } else if (this.data.s == 2) {
        userModel.myConsume({
          ...this.data,
        }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          if (data.list.length === 0) {
            this.setData({ busy: true })
          } else {
            this.setData({ busy: false })
            if (append) {
              this.setData({ list: this.data.list.concat(data.list) })
            } else {
              this.setData({ list: data.list })
            }
          }
        })
      } else if (this.data.s == 3) {
        userModel.myRefill({
          ...this.data,
        }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          if (data.list.length === 0) {
            this.setData({ busy: true })
          } else {
            this.setData({ busy: false })
            if (append) {
              this.setData({ list: this.data.list.concat(data.list) })
            } else {
              this.setData({ list: data.list })
            }
          }
        })
      } else if (this.data.s == 4) {
        userModel.myBalanceLog({
          ...this.data,
        }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          if (data.list.length === 0) {
            this.setData({ busy: true })
          } else {
            this.setData({ busy: false })
            if (append) {
              this.setData({ list: this.data.list.concat(data.list) })
            } else {
              this.setData({ list: data.list })
            }
          }
        })
      } else if (this.data.s == 5) {
        userModel.myTransfer({
          ...this.data,
        }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          if (data.list.length === 0) {
            this.setData({ busy: true })
          } else {
            this.setData({ busy: false })
            if (append) {
              this.setData({ list: this.data.list.concat(data.list) })
            } else {
              this.setData({ list: data.list })
            }
          }
        })
      }
    }
  },

  handleClick(e) {
    const s = e.currentTarget.dataset.s
    if (s != this.data.s) {
      this.setData({
        s,
        page: 0,
        pageSize: 10,
        list: [],
        busy: false,
        type: 'balance'
      })
      this._getList()
    }
  },

  onAwait() {
    wx.navigateTo({
      url: '/sub_service/pages/commission/index?type=2'
    })
  }

});
